#include <dirent.h>
#include <unistd.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>



#define MAX_PATH 1024

typedef int (myfunc)(const char * fullpath);


void getAllYUVFileinDir(const char *path,myfunc func)
{
   
     
      DIR * dir;
      char  fullfilename[MAX_PATH]={0};
      struct dirent * dir_ptr;
      dir = opendir(path); /* 打开目录 */
      if(dir == NULL){
	    printf("fail to open dir[%s],errno:%d,info:%s.\n",path,errno,strerror(errno));
	    return;
      }

      
      while((dir_ptr = readdir(dir)) != NULL){ /* 顺序读取每一个目录项 */
		/* 跳过".."和"."两个目录 */
		if(strcmp(dir_ptr->d_name,".") == 0 || strcmp(dir_ptr->d_name,"..") == 0)
			continue;
		if(dir_ptr->d_type == DT_DIR){ /* 如果是目录，则递归调用get_file_count函数 */
			memset(fullfilename,0,MAX_PATH);
			snprintf(fullfilename,MAX_PATH,"%s/%s",path,dir_ptr->d_name);	
			getAllYUVFileinDir(fullfilename,func);
			
		}
		if(dir_ptr->d_type == DT_REG){ /* 如果是普通文件，则总数递增 */
		    char *ptr=strrchr(dir_ptr->d_name,'.');
		    if(ptr!=NULL&&strcmp(ptr,".java")==0){
		      {
			    memset(fullfilename,0,MAX_PATH);
			    snprintf(fullfilename,MAX_PATH,"%s/%s",path,dir_ptr->d_name);
			    func(fullfilename);
			
		      }
		    }
		}

	 }
	 closedir(dir);
	 return;
	
    
}

static int cpjavafiletoexchange(const char * filename)
{
	  
	  char exchangefile[MAX_PATH]={0};
	  char * ptr=NULL;
	  strncpy(exchangefile,filename,MAX_PATH);
	  ptr=strrchr(exchangefile,'.');
	  if(ptr==NULL)
	  {
	      printf("文件[%s]没有后缀名\n",exchangefile);
	      return -2;
	  }
	  if(strcmp(ptr,".java")!=0)
	  {
	      printf("文件[%s]不是java文件\n",exchangefile);
	      return -2;
	  }
	  sprintf(ptr,"%s",".exchange");
	  
	  int writefd=open(exchangefile,O_RDWR|O_CREAT|O_TRUNC);
	  if(writefd==-1)
	  {
	      printf("打开exchange文件[%s]失败,err:%d,info:%s\n",exchangefile,errno,strerror(errno));
	      return -3;
	  }
	  int readfd=open(filename,O_RDONLY);
	  if(readfd==-1)
	  {
	      printf("打开java[%s]文件失败,err:%d,info:%s\n",filename,errno,strerror(errno));
	      return -3;
	  }
// 	  char buffer[MAX_PATH]={0};
// 	  int len=0;
// 	  do
// 	  {
// 	      len=read(readfd,buffer,MAX_PATH);
// 	      if(len>0)
// 	      {
// 		  write(writefd,buffer,len);
// 	      }
// 	      
// 	  }while(len>0);
	  
	  close(writefd);
	  close(readfd);
	  printf("完成将[%s]复制到[%s]的处理\n",filename,exchangefile);
	  return 0;
	    
}

static int cpexchangetojavafile(const char * filename)
{
	  printf("处理文件[%s].\n",filename);
	  char javafile[MAX_PATH]={0};
	  const char * ptr=strrchr(filename,'.'); 
	  if(ptr==NULL)
	  {
	      printf("文件[%s]没有后缀名\n",filename);
	      return -1;
	  }
	  strncpy(javafile,filename,ptr-filename);
	  ptr=strrchr(javafile,'.');
	  if(ptr==NULL)
	  {
	      printf("文件[%s]没有后缀名\n",javafile);
	      return -2;
	  }
	  if(strcmp(ptr,".java")!=0)
	  {
	      printf("文件[%s]不是java文件\n",javafile);
	      return -2;
	  }
	  
	  int writefd=open(javafile,O_TRUNC|O_RDWR,0666);
	  if(writefd==-1)
	  {
	      printf("打开java文件[%s]失败\n",javafile);
	      return -3;
	  }
	  int readfd=open(filename,O_RDONLY,0666);
	  if(readfd==-1)
	  {
	      printf("打开exchange[%s]文件失败\n",filename);
	      return -3;
	  }
// 	  char buffer[MAX_PATH]={0};
// 	  int len=0;
// 	  do
// 	  {
// 	      len=read(readfd,buffer,MAX_PATH);
// 	      if(len>0)
// 	      {
// 		  write(writefd,buffer,len);
// 	      }
// 	      
// 	  }while(len>0);
	  
	  close(writefd);
	  close(readfd);
	  printf("完成将[%s]复制到[%s]的处理\n",filename,javafile);
	  return 0;
	    
}


int main(int argc,char * argv[])
{
	if(argc!=2)
	{
	    printf("请设置好工作目录,用法: mycrack 工作目录\n");
	    exit(-1);
	}
	printf("输入的文件夹:[%s]\n",argv[1]);
// 	getAllYUVFileinDir(argv[1],cpexchangetojavafile);
	getAllYUVFileinDir(argv[1],cpjavafiletoexchange);
	return 0;
}



